const app = getApp();
Component({
  data: {
    screenHeight: app.globalData.screenHeight,
    nodata: true,
    noform: true,
    show: true,
    startX: 0, //开始坐标
    startY: 0,
    page: 1,
    volunteerList:[]
  },
  methods: {
    naToInfo() {
      let volunteerList = this.data.volunteerList;
      // console.log(noform);
      if (volunteerList.length<6) {
        wx.navigateTo({
          url: '/pages/fill/chooseMajor/chooseMajor',
        })
      }

      // wx.navigateTo({
      //   url: '/pages/fill/myvolunteerList/myvolunteerList?id='+app.globalData.user.id,
      // })
      // if (!noform) {
      // } else {
      //   wx.navigateTo({
      //     url: '/pages/fill/chooseMajor/chooseMajor',
      //   })
      // }
    },
    fillInfo(e) {
      wx.navigateTo({
        url: '/pages/fill/fillform/fillform',
      })
    },
    getUserData(that, openId) {
      app.globalData.wxRequest.requestGet('volunteer/getUserDetail.do', {
        openId: openId
      }).then(res => {
        let nodata = false;
        if(!res.data.username){
          nodata = true
        }
        if (res.code == '201') {
          wx.showToast({
            title: res.msg,
            icon: 'error'
          })
          return
        }
        // let volunteerList = res.data.volunteerList
        const {
          volunteerList
        } = res.data
        volunteerList.map(item=>{
          item.isTouchMove = false
        })
        let noform = false;
        if (volunteerList.length == 6) {
          noform = true
        } else {
          noform = false
        }
        
        that.setData({
          userInfo: res.data,
          nodata: nodata,
          noform: noform,
          volunteerList
        })
      }).catch(err => {
        wx.showToast({
          title: '请求失败',
          icon: 'error'
        })
      })
    },
    editUserInfo(e) {
      wx.navigateTo({
        url: '/pages/fill/fillform/fillform?edit=1',
      })
    },
    toMyVolunteer(e) {
      let id = e.currentTarget.dataset.id;
      wx.navigateTo({
        url: '/pages/fill/myvolunteerList/myvolunteerList?id=' + id
      })
    },
    // 左滑删除相关
    touchE: function (e) {
      var that = this
      if (e.changedTouches.length == 1) {
        //手指移动结束后触摸点位置的X坐标
        var endX = e.changedTouches[0].clientX;
        //触摸开始与结束，手指移动的距离
        var disX = that.data.startX - endX;
        var delBtnWidth = that.data.delBtnWidth;
        //如果距离小于删除按钮的1/2，不显示删除按钮
        var txtStyle = disX > delBtnWidth / 2 ? "left:-" + delBtnWidth + "rpx" : "left:0rpx";

        //获取手指触摸的是哪一项
        var index = e.currentTarget.dataset.index;
        var volunteerList = that.data.volunteerList;
        volunteerList[index].txtStyle = txtStyle;
        //更新列表的状态
        that.setData({
          volunteerList: volunteerList
        });
      }
    },
    //手指触摸动作开始 记录起点X坐标
    touchstart: function (e) {
      //开始触摸时 重置所有删除
      this.data.volunteerList.forEach(function (v, i) {
        if (v.isTouchMove) //只操作为true的
          v.isTouchMove = false;
      })
      this.setData({
        startX: e.changedTouches[0].clientX,
        startY: e.changedTouches[0].clientY,
        volunteerList: this.data.volunteerList
      })
    },
    //滑动事件处理
    touchmove: function (e) {
      var that = this,
        index = e.currentTarget.dataset.index, //当前索引
        startX = that.data.startX, //开始X坐标
        startY = that.data.startY, //开始Y坐标
        touchMoveX = e.changedTouches[0].clientX, //滑动变化坐标
        touchMoveY = e.changedTouches[0].clientY, //滑动变化坐标
        //获取滑动角度
        angle = that.angle({
          X: startX,
          Y: startY
        }, {
          X: touchMoveX,
          Y: touchMoveY
        });
      that.data.volunteerList.forEach(function (v, i) {
        v.isTouchMove = false
        //滑动超过30度角 return
        if (Math.abs(angle) > 30) return;
        if (i == index) {
          if (touchMoveX > startX) //右滑
            v.isTouchMove = false
          else //左滑
            v.isTouchMove = true
        }
      })
      //更新数据
      that.setData({
        volunteerList: that.data.volunteerList
      })
    },
    /**
     * 计算滑动角度
     * @param {Object} start 起点坐标
     * @param {Object} end 终点坐标
     */
    angle: function (start, end) {
      var _X = end.X - start.X,
        _Y = end.Y - start.Y
      //返回角度 /Math.atan()返回数字的反正切值
      return 360 * Math.atan(_Y / _X) / (2 * Math.PI);
    },
    delBtn(e){
      let that = this;
      let id = e.currentTarget.id;
      let openId = app.globalData.openId;
      app.globalData.wxRequest.requestGet('volunteer/deleteVolunteer.do', {
        id: id
      }).then(res => {
        if (res.code == '201') {
          wx.showToast({
            title: res.msg,
            icon: 'error'
          })
          return
        }
        if (res.code == '200') {
          that.getUserData(that,openId)
          wx.showToast({
            title: res.msg,
            icon: 'success'
          })
          return
        }
      }).catch(err => {
        wx.showToast({
          title: '请求失败',
          icon: 'error'
        })
      })
    }
  },
  created: function () {
    // console.log('[my-component] created')
  },
  pageLifetimes: {
    show: function () {
      // 页面被展示
      let that = this;
      // console.log('show');
      let openId = app.globalData.openId;
      // let openId = 'o0sP25CWPn1R2lhJvIaQ-bwjVfpM';
      that.getUserData(that, openId)

      let isSignUp = app.globalData.isSignUp;
      // console.log('isSignUp', isSignUp, that.data.noform);
      if (isSignUp) {
        that.data.noform = false;
        // console.log('isSignUp', isSignUp, that.data.noform);
        // console.log('[my-component----------] created', that.data)
      }
    },
    hide: function () {
      // 页面被隐藏
    },
    resize: function (size) {
      // 页面尺寸变化
    }
  }

})